'''
Created on 8 oct. 2012

@author: MARTINS-N
'''

import utils
import login_client
import sys
import threading
import database
import __builtin__
from socket import *

#Constants
PYFUS_VERSION = "1.0.0.0"
SETTINGS = {
            "LogonHost":"127.0.0.1",
            "LogonPort":444,
            "GameHost":"127.0.0.1",
            "GamePort":5556,
            }

#Variables
DB = database.db()
LOGON_SERVER = None
LOGON_CLIENTS = []
GAME_SERVER = None
GAME_CLIENTS = []
SWITCH_TICKETS = []

def start_network():
    print 'Starting network services ..'
    LOGON_SERVER = socket(AF_INET, SOCK_STREAM)
    addr = (SETTINGS['LogonHost'], SETTINGS['LogonPort'])
    LOGON_SERVER.bind(addr)
    LOGON_SERVER.listen(1000)#Max connection allowed
    
    GAME_SERVER = socket(AF_INET, SOCK_STREAM)
    addr = (SETTINGS['GameHost'], SETTINGS['GamePort'])
    GAME_SERVER.bind(addr)
    GAME_SERVER.listen(1000)#Max connection allowed
    
    #Start the thread
    threading.Thread(None, acceptLogonConnection, None, (LOGON_SERVER, LOGON_CLIENTS), {}).start()
    threading.Thread(None, acceptGameConnection, None, (GAME_SERVER, GAME_CLIENTS), {}).start()
    print 'Network services started'
    pass

def acceptLogonConnection(server, list):
    while(True):
        sock,addr = server.accept()
        print 'Received connection on logonserver ...'
        client = login_client.login_client(sock)
        list.append(client)
        pass
    pass

def acceptGameConnection(server, list):
    while(True):
        sock,addr = server.accept()
        print 'Received connection on gameserver ...'
        #client = login_client.login_client(sock)
        #list.append(client)
        pass
    pass

###################
### Main Method ###
###################
if __name__ == '__main__':
    print 'Running Pyfus v' + PYFUS_VERSION + " by NightWolf"
    start_network()
    print 'Pyfus started !'
    while(True):
        user_cmd = raw_input("")
        pass
    pass